/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.GUI.graphic;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import org.w3c.dom.*;
import java.util.*;
import java.awt.*;
import javax.swing.JOptionPane;
import view.GUI.LabelReader;
/**
 *
 * @author Ben
 */
public class SkinReader {
    private Map<String, SkinInfo> info;
    private static SkinReader reader = null;
    private String currentSkin;

    public static String getSkinPath(){
        return "skins/";
    }

    private synchronized static void createInstance () {
        try{
            if (reader == null){
                reader = new SkinReader("default");
            }
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null,"SkinReader:" + ex.getMessage(),
              LabelReader.getInstance().getString("Error") , JOptionPane.ERROR_MESSAGE);
        }
    }

    public static SkinReader getInstance () {
        if (reader == null){
            createInstance ();
        }
        return reader;
    }


    public SkinReader(String skinName) throws Exception{
        info = new HashMap<String, SkinInfo>();
        setSkin(skinName);
    }

    public void setSkin(String skinName) throws Exception{
        String file= getSkinPath() + skinName + "/index.xml";
        currentSkin = skinName;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File(file));
        NodeList componentKnopen= document.getElementsByTagName("Component");
        info.clear();
        for (int i=0;i < componentKnopen.getLength();i++){
                Node component=componentKnopen.item(i);
                SkinInfo skinInfo = createSkinInfoObject(component);
                info.put(skinInfo.getSimpleName(),skinInfo);
        }
    }

    public SkinInfo getSkinInfo(String simpleName){
       SkinInfo ret = info.get(simpleName);
       if(ret==null){
           ret = new SkinInfo(simpleName, Color.WHITE, Color.WHITE, null, false,
                             new Font("Arial",Font.PLAIN,10), Color.BLACK);
       }
       return ret;
    }

    private SkinInfo createSkinInfoObject(Node componentNode) throws Exception{
        NodeList children=componentNode.getChildNodes();
        String simpleName = null;
        Image background = null;
        Color color1 = null, color2 = null, fontColor = null;
        boolean reflect = false;
        Font font = null;
        for (int i=0;i<children.getLength();i++){
            Node child=children.item(i);
            if (child.getNodeName().equals("SimpleName")){
                simpleName=child.getFirstChild().getNodeValue();
            } else if (child.getNodeName().equals("Color1")){
                color1= new Color(Integer.parseInt(child.getFirstChild().getNodeValue()));
            } else if(child.getNodeName().equals("Color2")){
                color2= new Color(Integer.parseInt(child.getFirstChild().getNodeValue()));
            } else if(child.getNodeName().equals("Reflect")){
                reflect = Boolean.parseBoolean(child.getFirstChild().getNodeValue());
            } else if (child.getNodeName().equals("BackgroundImg")){
                background=Toolkit.getDefaultToolkit().createImage(getSkinPath() +
                        currentSkin + "/"+ child.getFirstChild().getNodeValue());
            } else if (child.getNodeName().equals("Font")){
                font = new Font(child.getFirstChild().getNodeValue(), Font.PLAIN,
                           Integer.parseInt(child.getAttributes().getNamedItem("Size").getNodeValue()));
                fontColor = new Color(Integer.parseInt(child.getAttributes().getNamedItem("Color").getNodeValue()));
            }
        }
        return new SkinInfo(simpleName, color1, color2, background, reflect,font, fontColor);
    }
}
